<template>
	<view class="preview" v-if="wallInfo">
		<swiper circular :current="wallIndex" @change="changeWall">
			<swiper-item v-for="(item, index) in wallList" :key="item._id">
				<image v-if="loadWalls.includes(index)" @click="maskShow" :src="item.picurl" mode="aspectFill"></image>
			</swiper-item>
		</swiper>
		<view class="mask" v-if="maskState">
			<view class="goBack" :style="{ top: getStatusBarHeight() + 'px' }" @click="goBack">
				<uni-icons type="back" size="24" color="#eee"></uni-icons>
			</view>
			<view class="count">{{ wallIndex + 1 }} / {{ wallList.length }}</view>
			<view class="time">
				<uni-dateformat :date="Date.now()" format="hh:mm"></uni-dateformat>
			</view>
			<view class="date">
				<uni-dateformat :date="Date.now()" format="MM月dd日"></uni-dateformat>
			</view>
			<view class="footer">
				<view class="box" @click="showDescription">
					<uni-icons type="info" size="28"></uni-icons>
					<view class="text">信息</view>
				</view>
				<view class="box" @click="showScore">
					<uni-icons type="star" size="28"></uni-icons>
					<view class="text">{{ wallInfo.score }}分</view>
				</view>
				<view class="box">
					<uni-icons type="download" size="28" @click="downloadWall"></uni-icons>
					<view class="text">下载</view>
				</view>
			</view>
		</view>

		<uni-popup ref="desPopup" type="bottom">
			<view class="desPopup">
				<view class="popHeader">
					<view></view>
					<view class="title">壁纸信息</view>
					<view class="close" @click="closeDescription">
						<uni-icons type="closeempty" size="18" color="#999"></uni-icons>
					</view>
				</view>
				<scroll-view scroll-y>
					<view class="content">
						<view class="row">
							<view class="label">壁纸ID:</view>
							<text class="value">{{ wallInfo._id }}</text>
						</view>
						<!-- <view class="row">
							<view class="label">分类:</view>
							<text class="value classify">{{ wallInfo }}</text>
						</view> -->
						<view class="row">
							<view class="label">发布者:</view>
							<text class="value">{{ wallInfo.nickname }}</text>
						</view>
						<view class="row">
							<view class="label">评分:</view>
							<view class="value roteBox">
								<uni-rate allow-half readonly :value="wallInfo.score" size="20" />
								<text class="score">{{ wallInfo.score }}分</text>
							</view>
						</view>
						<view class="row">
							<view class="label">摘要:</view>
							<view class="value">{{ wallInfo.description }}</view>
						</view>
						<view class="row">
							<view class="label">标签:</view>
							<view class="value tabs">
								<view class="tab" v-for="item in wallInfo.tabs" :key="item._id">
									{{ item }}
								</view>
							</view>
						</view>
						<view class="copyright">声明：撒旦法收到发生的发色发的顺丰</view>
						<view class="safe-area-inset-bottom"></view>
					</view>
				</scroll-view>
			</view>
		</uni-popup>

		<uni-popup ref="scorePopup" type="center" :is-mask-click="false">
			<view class="scorePopup">
				<view class="popHeader">
					<view></view>
					<view class="title">
						{{ scoreStatus ? "已经评分了~" : "壁纸评分" }}
					</view>
					<view class="close" @click="closeScore">
						<uni-icons type="closeempty" size="18" color="#999"></uni-icons>
					</view>
				</view>
				<view class="score">
					<uni-rate allowHalf class="uni-rate" size="30" v-model="userScore" :readonly="scoreStatus" />
					<text class="text">{{ userScore }}分</text>
				</view>
				<view class="button">
					<button @click="setScore" type="default" size="mini" plain :disabled="!userScore || scoreStatus">
						确认评分
					</button>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script setup>
	import {
		ref
	} from "vue";
	import {
		getStatusBarHeight,
		getNavBarHeight
	} from "@/utils/system.js";
	import {
		onLoad
	} from "@dcloudio/uni-app";
	import {
		apiGetWallDetail,
		apiSetupScore,
		apiWriteDownload,
	} from "@/api/apis.js";
	const maskState = ref(true);
	const desPopup = ref(null);
	const scorePopup = ref(null);
	const userScore = ref(0);
	const scoreStatus = ref(false);
	const wallId = ref(0);
	const wallList = ref([]);
	const wallListCache = uni.getStorageSync("wallListCache") || [];
	const wallIndex = ref(0);
	const loadWalls = ref([]);
	const wallInfo = ref({});

	onLoad((e) => {
		wallId.value = e.id;
		if (!wallId.value) {
			setTimeout(() => {
				uni.showToast({
					title: "预览时未传入id，返回上一页",
					icon: "none",
					mask: true,
					complete() {
						setTimeout(() => {
							uni.navigateBack();
						}, 1500);
					},
				});
			}, 500);

			return;
		}
		wallList.value = wallListCache.map((item) => {
			return {
				...item,
				picurl: item.smallPicurl.replace("_small.webp", ".jpg"),
			};
		});
		wallIndex.value = wallList.value.findIndex((item) => item._id == e.id);
		wallInfo.value = wallList.value[wallIndex.value];
		userScore.value = wallInfo.value.userScore || 0;
		scoreStatus.value = userScore.value > 0;
		pushWallIndex();
	});

	const pushWallIndex = () => {
		loadWalls.value.push(
			wallIndex.value <= 0 ? wallList.value.length - 1 : wallIndex.value - 1,
			wallIndex.value,
			wallIndex.value >= wallList.value.length - 1 ? 0 : wallIndex.value + 1
		);
		loadWalls.value = [...new Set(loadWalls.value)];
	};
	const changeWall = (e) => {
		wallIndex.value = e.detail.current;
		wallInfo.value = wallList.value[wallIndex.value];
		userScore.value = wallInfo.value.userScore || 0;
		scoreStatus.value = userScore.value > 0;
		pushWallIndex();
	};
	const maskShow = () => {
		maskState.value = !maskState.value;
	};
	const showDescription = () => {
		desPopup.value.open();
	};
	const closeDescription = () => {
		desPopup.value.close();
	};

	const showScore = () => {
		scorePopup.value.open();
	};
	const closeScore = () => {
		scorePopup.value.close();
	};
	const setScore = async () => {
		uni.showLoading({
			title: "加载中",
		});
		let {
			classid,
			_id: wallId
		} = wallInfo.value;
		let res = await apiSetupScore({
			classid,
			wallId,
			userScore: userScore.value,
		});
		uni.hideLoading();
		if (res.errCode == 0) {
			uni.showToast({
				title: "评分成功",
			});
			wallList.value[wallIndex.value].userScore = userScore.value;
			uni.setStorageSync("wallListCache", wallList.value);
			closeScore();
			scoreStatus.value = true;
		}
	};
	const goBack = () => {
		uni.navigateBack();
	};
	const downloadWall = async () => {
		// #ifdef H5
		uni.showModal({
			title: "请长按保存图片",
			icon: "none",
		});
		// #endif
		// #ifndef H5
		try {
			uni.showLoading({
				title: "下载中...",
				mask: true,
			});
			let {
				classid,
				_id: wallId
			} = wallInfo.value;
			let res = await apiWriteDownload({
				classid,
				wallId,
			});
			if (res.errCode != 0) throw res;
			uni.getImageInfo({
				src: wallInfo.value.picurl,
				success(res) {
					uni.saveImageToPhotosAlbum({
						filePath: res.path,
						success() {
							uni.showToast({
								title: "保存成功",
							});
						},
						fail(res) {
							if (res.errMsg == "saveImageToPhotosAlbum:fail cancel") {
								uni.showToast({
									title: "已取消下载",
									icon: "error",
								});
								return;
							}
							uni.showModal({
								title: "授权提示",
								content: "需要授权保存相册",
								success(res) {
									if (res.confirm) {
										uni.openSetting({
											success(setting) {
												if (setting.authSetting[
														"scope.writePhotosAlbum"
														]) {
													uni.showToast({
														title: "获取授权成功",
													});
												} else {
													uni.showToast({
														title: "获取授权失败",
														icon: "error",
													});
												}
											},
										});
									} else {
										uni.showToast({
											title: "获取授权失败",
											icon: "error",
										});
									}
								},
							});
						},
						complete() {
							uni.hideLoading();
						},
					});
				},
			});
		} catch (err) {
			console.log(err);
			uni.hideLoading();
		}
		// #endif
	};
</script>

<style lang="scss" scoped>
	.preview {
		width: 100%;
		height: 100vh;
		position: relative;

		swiper {
			width: 100%;
			height: 100%;

			image {
				width: 100%;
				height: 100%;
			}
		}

		.mask {
			&>view {
				position: absolute;
				width: fit-content; // 自动按内容长度填充
				left: 0;
				right: 0;
				margin: auto;
				color: white;
			}

			.goBack {
				width: 38px;
				height: 38px;
				background: rgba(0, 0, 0, 0.5);
				left: 30rpx;
				margin-left: 0;
				border-radius: 100px;
				top: 0;
				backdrop-filter: blur(10rpx);
				border: 1rpx solid rgba(255, 255, 255, 0.3);
				display: flex;
				justify-content: center;
				align-items: center;
			}

			.count {
				top: 10vh;
				background-color: rgba(0, 0, 0, 0.3);
				font-size: 28rpx;
				border-radius: 40rpx;
				padding: 8rpx 28rpx;
				backdrop-filter: blur(10rpx);
			}

			.time {
				font-size: 140rpx;
				top: calc(10vh + 80rpx);
				font-weight: 100;
				line-height: 1em;
				text-shadow: 0 4rpx rgba(0, 0, 0, 0.3);
			}

			.date {
				font-size: 34rpx;
				top: calc(10vh + 230rpx);
				text-shadow: 0 2rpx rgba(0, 0, 0, 0.3);
			}

			.footer {
				background: rgba(255, 255, 255, 0.8);
				bottom: 10vh;
				width: 65vw;
				height: 120rpx;
				border-radius: 120rpx;
				color: #000;
				display: flex;
				justify-content: space-around;
				align-items: center;
				box-shadow: 0 2rpx rgba(0, 0, 0, 0.1);
				backdrop-filter: blur(20rpx);

				.box {
					display: flex;
					flex-direction: column;
					align-items: center;
					justify-content: center;
					padding: 2rpx 12rpx;

					.text {
						font-size: 26;
						color: $text-font-color-2;
					}
				}
			}
		}

		.popHeader {
			display: flex;
			justify-content: space-between;
			align-items: center;

			.title {
				color: $text-font-color-2;
				font-size: 26rpx;
			}

			.close {
				padding: 6rpx;
			}
		}

		.desPopup {
			background: #fff;
			padding: 30rpx;
			border-radius: 30rpx 30rpx 0;
			overflow: hidden;

			scroll-view {
				max-height: 60vh;

				.content {
					.row {
						display: flex;
						padding: 16rpx 0;
						font-size: 28rpx;
						line-height: 1.7em;

						.label {
							color: $text-font-color-3;
							width: 140rpx;
							text-align: right;
							font-size: 30rpx;
						}

						.value {
							flex: 1;
							width: 0;
							padding-left: 10rpx;
						}

						.roteBox {
							display: flex;
							align-items: center;

							.score {
								font-size: 26rpx;
								color: $text-font-color-2;
								padding-left: 10rpx;
							}
						}

						.tabs {
							display: flex;
							flex-wrap: wrap;

							.tab {
								border: 1px solid $brand-theme-color;
								color: $brand-theme-color;
								font-size: 22rpx;
								padding: 10rpx 30rpx;
								border-radius: 40rpx;
								line-height: 1em;
								margin: 5rpx;
							}
						}

						.classify {
							color: $brand-theme-color;
						}
					}

					.copyright {
						font-size: 22rpx;
						padding: 20rpx;
						background: $text-font-color-4;
						color: $text-font-color-2;
						border-radius: 10rpx;
						margin: 20rpx 0;
						line-height: 1.6em;
					}
				}
			}
		}

		.scorePopup {
			background: #fff;
			padding: 30rpx;
			border-radius: 30rpx;
			overflow: hidden;
			width: 70vw;
			height: 340rpx;

			.score {
				display: flex;
				align-items: center;
				justify-content: center;
				padding: 30rpx 0;

				.text {
					padding-left: 10rpx;
					color: rgba(239, 204, 62, 0.9);
					width: 80rpx;
					line-height: 1em;
					text-align: right;
				}
			}

			.button {
				display: flex;
				align-items: center;
				justify-content: center;
				padding: 30rpx;
			}
		}
	}
</style>